<script setup lang="ts">
import { reactive,onMounted ,ref } from "vue";
import dayjs from 'dayjs';
import type {DateDataType} from "./index.d"
import {useSettingStore} from "@/stores/index"
import { useRouter, useRoute } from 'vue-router'
import datepick from "./index/datepicker.vue";

const router = useRouter()
const route = useRoute()

const dateData = reactive<DateDataType>({
  dateDay: "",
  dateYear: "",
  dateWeek: "",
  timing:null
});

const { setSettingShow} =useSettingStore()
const weekday= ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
const timeFn = () => {
  dateData.timing = setInterval(() => {
    dateData.dateDay = dayjs().format("YYYY-MM-DD hh : mm : ss");
    dateData.dateWeek = weekday[dayjs().day()];
  }, 1000);
};
timeFn()
const goBack= () => {
  router.go(-1); // 等同于浏览器的返回按钮
}
// 退出
const signOut = () => {
    localStorage.clear();
    router.replace({ path: '/login' });
    // 刷新页面
    //router.go(0)
}
// 点击返回上一页
const handleBack = () => {
  // 等同于浏览器的「后退」按钮，返回历史记录的上一页
  router.back();
  // 也可以用 router.go(-1)，效果完全相同
  // router.go(-1);
};
const username = ref('');
  const companyvalue = ref('')
  const companylists = ref()
  // 2. 定义v-model绑定的变量
const selectedValue = ref('');
onMounted(() => {
  // 确保在客户端执行
  if (typeof window !== 'undefined') {
    
    username.value = JSON.parse(localStorage.getItem('username') )|| '游客';
    const companylist = localStorage.getItem('companys')
    const compan=JSON.parse(localStorage.getItem('company'));
    if (companylist) {
  // 将字符串字符串转换为JavaScript对象
  const tempvalue=JSON.parse(companylist);
  companylists.value = tempvalue;
  
  if(tempvalue.length>0){



// 判断数组中是否存在 id 为 2 的对象
const exists = tempvalue.some(item => item.company_id === compan);

    if(!exists){
      selectedValue.value=tempvalue[0].company_id;
    }else{
      selectedValue.value=compan
    }
    
  }
  
    }
    
    // 重命名属性
//renameRefKey(companylists, 'company_id', 'value');
// 重命名属性
//renameRefKey(companylists, 'company_name', 'label');
    debugger;
  }
  });


// 工具函数：重命名ref对象中的属性
function renameRefKey(refObj, oldKey, newKey) {
  // 检查参数有效性
  if (!refObj || !refObj.value || typeof refObj.value !== 'object') {
    console.error('第一个参数必须是包含对象的ref');
    return false;
  }
  
  // 检查旧属性是否存在
  if (!(oldKey in refObj.value)) {
    console.warn(`对象中不存在属性: ${oldKey}`);
    return false;
  }
  
  // 检查新属性是否已存在
  if (newKey in refObj.value) {
    console.warn(`对象中已存在属性: ${newKey}，将被覆盖`);
  }
  
  // 重命名属性：复制值到新属性，然后删除旧属性
  refObj.value[newKey] = refObj.value[oldKey];
  delete refObj.value[oldKey];
  
  return true;
}

function selectChange(value){
  debugger;
  const comp=localStorage.getItem('company');
  if(comp!=value){
    localStorage.setItem('company',value);
    window.location.reload();
  }
  //alert(value)
  
}
  
</script>

<template>
  <div class="d-flex jc-center title_wrap">
    <!-- 左上角日期选择器 -->
    <div class="datepicker-container">
      <datepick/>
          
       <el-select
    v-model="selectedValue"
    class="m-2"
    placeholder="Select"
    style=" margin: 0.5rem 0.5rem 0.5rem 0rem"
    @change="selectChange"
  >
    <el-option
      v-for="item in companylists"
      :key="item.company_id"
      :label="item.company_name"
      :value="item.company_id"
    />
  </el-select>
 
    </div>
    
    <!-- 原有的左侧装饰元素 -->
    <div class="zuojuxing"></div>
    <div class="youjuxing"></div>
    <div class="guang"></div>
    
    <!-- 标题 -->
    <div class="d-flex jc-center">
      <div class="title">
        <span class="title-text">可视化监控平台</span>
      </div>
    </div>

    <!-- 右侧计时器和操作按钮 -->
    <div class="timers">
      登录人：[{{ username }}]{{ dateData.dateYear }} {{ dateData.dateWeek }} {{ dateData.dateDay }}
      
      <div class="setting_icon" @click="setSettingShow(true)">
        <img src="@/assets/img/headers/setting.png" alt="设置">
      </div>
      <div class="setting_icon" @click="handleBack()">
        <img src="@/assets/img/headers/back.png" alt="返回">
      </div>
      <div class="setting_icon" @click="signOut()">
        <img src="@/assets/img/headers/out.png" alt="退出">
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.title_wrap {
  height: 60px;
  background-image: url("../assets/img/top.png");
  background-size: cover;
  background-position: center center;
  position: relative;
  margin-bottom: 4px;

  .guang {
    position: absolute;
    bottom: -26px;
    background-image: url("../assets/img/guang.png");
    background-position: 80px center;
    width: 100%;
    height: 56px;
  }

  .zuojuxing,
  .youjuxing {
    position: absolute;
    top: -2px;
    width: 140px;
    height: 6px;
    background-image: url("../assets/img/headers/juxing1.png");
  }

  .zuojuxing {
    left: 11%;
  }

  .youjuxing {
    right: 11%;
    transform: rotate(180deg);
  }

  .timers {
    position: absolute;
    right: 0;
    top: 30px;
    font-size: 18px;
    display: flex;
    align-items: center;

    .setting_icon {
      width: 20px;
      height: 20px;
      cursor: pointer;
      margin-left: 12px;
      img{
        width: 100%;
        height: 100%;
      }
    }
  }
}
.title {
  position: relative;
  // width: 500px;
  text-align: center;
  background-size: cover;
  color: transparent;
  height: 60px;
  line-height: 46px;

  .title-text {
    font-size: 38px;
    font-weight: 900;
    letter-spacing: 6px;
    width: 100%;
    background: linear-gradient(
      92deg,
      #0072ff 0%,
      #00eaff 48.8525390625%,
      #01aaff 100%
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
}
.datepicker-container {
  position: absolute;
  top: -10px;
  left: 10px;
  z-index: 10; // 确保在其他元素之上
  // 如果需要添加额外样式（如背景、边框等）
  // background: rgba(255, 255, 255, 0.8);
  // padding: 8px;
  // border-radius: 4px;
}
</style>
